package com.example.food_contract_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.food_contract_system.domain.Contracts;
import com.example.food_contract_system.service.ContractsService;
import com.example.food_contract_system.mapper.ContractsMapper;
import com.example.food_contract_system.util.R;
import com.example.food_contract_system.vo.AddContractVo;
import com.example.food_contract_system.vo.ContractUpdateAndDeleteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* @author Lhy
* @description 针对表【contracts】的数据库操作Service实现
* @createDate 2023-07-06 09:22:08
*/
@Service
public class ContractsServiceImpl extends ServiceImpl<ContractsMapper, Contracts> implements ContractsService{

    @Autowired
    private ContractsMapper contractsMapper;

    @Transactional
    @Override
    public R addContract(AddContractVo addContractVo) {
        Contracts contracts = new Contracts();

        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        contracts.setCId(s.substring(0,4));
        contracts.setBId(addContractVo.getBId());

        Date date = new Date();
        // 创建SimpleDateFormat对象，指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 格式化日期
        String formattedDate = sdf.format(date);
        contracts.setCUploadTime(formattedDate);

        contracts.setCSrc("http://localhost:8081/index/"+addContractVo.getCSrc());
        contracts.setCName(addContractVo.getCName().split("\\.")[0]);

        int insert = contractsMapper.insert(contracts);

        if (insert > 0){
            return R.ok().put("msg","上传成功!");
        }

        return R.error();
    }

    @Override
    public R getContractList() {
        List<Contracts> contracts = contractsMapper.selectList(null);
        return R.ok().put("data",contracts);
    }

    @Override
    public R updateContract(ContractUpdateAndDeleteVo contractUpdateAndDeleteVo) {
        // 1.判断是否存在主合同
        Contracts contracts = contractsMapper.selectOne(new QueryWrapper<Contracts>()
                .eq("c_status", "0"));
        if (contracts == null){
            // 没有主合同,可以添加主合同
            int i = contractsMapper.updateByIdMyself(contractUpdateAndDeleteVo.getId(),"0");
            if (i > 0){
                return R.ok().put("msg","更新成功");
            }else{
                return R.error(400,"请联系管理员!");
            }
        }else{
            // 判断是不是要改变 主合同的级别
            if (contracts.getCId().equals(contractUpdateAndDeleteVo.getId())){
                // 是更改主合同级别
                int i = contractsMapper.updateByIdMyself(contractUpdateAndDeleteVo.getId(),"1");
                if (i > 0){
                    return R.ok().put("msg","更新成功");
                }else{
                    return R.error(400,"请联系管理员!");
                }
            }else{
                return R.error(500,"已经有主合同了");
            }
        }
    }

    @Override
    public R deleteContract(ContractUpdateAndDeleteVo contractUpdateAndDeleteVo) {
        int id = contractsMapper.delete(new QueryWrapper<Contracts>()
                .eq("c_id", contractUpdateAndDeleteVo.getId()));
        if (id > 0){
            return R.ok().put("msg","删除成功!");
        }
        return R.error();
    }

    @Override
    public R getMainContract() {
        Contracts contracts = contractsMapper.selectOne(new QueryWrapper<Contracts>()
                .eq("c_status", '0'));
        if (contracts != null){
            return R.ok().put("data",contracts);
        }
        return R.error();
    }
}




